"""
各种小部件使用的例子的UI层。
"""
import asyncio
import typing

from elio import core


class WidgetLayer(core.AutoRecycleLayer[core.Activity]):
    llContainer: core.ListLayout = None

    async def on_create(self, *args: typing.Any, **kwargs: typing.Dict):
        await super().on_create(*args, *kwargs)

    async def on_show(self):
        await super().on_show()

        activity = self.parent
        self.llContainer = await core.ListLayout.create(activity)
        # 创建小部件
        we_test = await self.llContainer.add(core.WidgetEditor, .1, widget_args=('sscn.byethost3.com',))
        wb_test = await self.llContainer.add(core.WidgetButton, .1, widget_args=('测试',))
        wt_test = await self.llContainer.add(core.WidgetText, .1, widget_args=('测试一下文本显示',))
        wcb_test = await self.llContainer.add(core.WidgetCheckBox, .1, widget_args=('记住选择',))
        rl_test = await self.llContainer.add(core.RowLayout, .1)
        wr_test1 = await rl_test.add(core.WidgetRadio, .5, widget_args=('单选钮1',))
        wr_test2 = await rl_test.add(core.WidgetRadio, .5, widget_args=('单选钮2',))
        ws_test = await self.llContainer.add(core.WidgetSpinner, .1, widget_args=(True,))
        wi_test = await self.llContainer.add(core.WidgetImage, .2, widget_args=(
            'https://img2.baidu.com/it/u=1211232624,1554315570&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500',
        ))

        # 编辑器小部件测试
        await we_test.set_hint('请输入账号')
        print('EditorHint', await we_test.get_hint())

        # 按钮小部件测试
        async def cb_btn():
            """
            按钮点击事件。
            """
            print('Button clicked.')
            print('EditorText', await we_test.get_text())
            await wb_test.set_text('123')
            print('ButtonText', await wb_test.get_text())
            await wb_test.set_accessibility_label('456')
            print('ButtonLabel', await wb_test.get_accessibility_label())

        await wb_test.set_on_click_listener(cb_btn)
        print('ButtonRegion', await wb_test.get_region())

        # 复选框小部件测试
        await wcb_test.set_checked(True)
        print('CheckBoxChecked', await wcb_test.is_checked())

        # 控件动态设置位置测试
        await asyncio.sleep(2)
        await wt_test.set_location(200, 600)

        # 设置小部件背景色测试
        # 安卓端无法识别RGBA
        await wt_test.set_background_color('#3535FF6F')

        # 设置小部件长按的监听函数测试
        async def cb_long():
            """
            长按事件。
            """
            print('Text long clicked.')

        await wt_test.set_on_long_click_listener(cb_long)

        # 单选钮小部件测试
        async def cb_radio(widget: core.WidgetRadio):
            """
            单选钮点击事件。
            """
            if widget == wr_test1:
                await wr_test2.set_checked(False)
            elif widget == wr_test2:
                await wr_test1.set_checked(False)

        await wr_test1.set_checked(True)
        await rl_test.set_on_click_listener(cb_radio)

        # 下拉框测试
        await ws_test.set_items(['橘子', '苹果', '香蕉'])

        async def cb_selected(index: int):
            """
            项目选择事件。
            """
            print('Spinner item selected', index)

        await ws_test.set_prompt('请选择分类')
        print(await ws_test.get_prompt())
        await ws_test.set_on_item_selected_listener(cb_selected)
        await ws_test.select(1)

        # 图像小部件测试
        await wi_test.set_accessibility_label('这是一张图片，点击清除图像')

        async def cb_image():
            """
            图像点击事件。
            """
            await wi_test.set_src(None)
            print('Image', await wi_test.get_src())

        await wi_test.set_on_click_listener(cb_image)

    async def on_hide(self):
        if self.llContainer:
            await self.llContainer.destroy()
        await super().on_hide()

    async def on_pause(self):
        await super().on_pause()

    async def on_resume(self):
        await super().on_resume()

    async def on_destroy(self, *args: typing.Any, **kwargs: typing.Dict):
        await super().on_destroy(*args, **kwargs)
